//
//  AppDelegate.m
//  TangYi
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 赵连鹏. All rights reserved.
//

#import "AppDelegate.h"
#import <AFNetworking.h>
#import "ZLPHomeViewController.h"
#import "ZLPFindViewController.h"
#import "ZLPUserViewController.h"
#import "ZLPSearchViewController.h"
#import "ZLPSaveTool.h"
#import "ZLPDataBaseHandle.h"
#import "UMSocial.h"
#import "ZLPGuideViewController.h"
#import "ZLPIsLoginHandle.h"
#import <MBProgressHUD.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    
    ZLPHomeViewController *homeVC = [[ZLPHomeViewController alloc] init];
    UINavigationController *homeNaVC = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"homepage.png"] tag:1000] autorelease];
    
    ZLPFindViewController *findVC = [[ZLPFindViewController alloc] init];
    UINavigationController *findNaVC = [[UINavigationController alloc] initWithRootViewController:findVC];
    findNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"find.png"] tag:1001] autorelease];
    
    ZLPUserViewController *userVC = [[ZLPUserViewController alloc] init];
    UINavigationController *userNaVC = [[UINavigationController alloc] initWithRootViewController:userVC];
    userNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"个人中心" image:[UIImage imageNamed:@"user.png"] tag:1002] autorelease];
    
    ZLPSearchViewController *searchVC = [[ZLPSearchViewController alloc] init];
    UINavigationController *searchNaVC = [[UINavigationController alloc] initWithRootViewController:searchVC];
    searchNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"搜索" image:[UIImage imageNamed:@"search.png"] tag:1003] autorelease];
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = @[homeNaVC, findNaVC, searchNaVC, userNaVC];
    
    self.window.rootViewController = tab;
    
//    // 友好界面
//    CGSize size = [UIScreen mainScreen].bounds.size;
//    
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
//    imageView.image = [UIImage imageNamed:@"saber.jpeg"];
//    
//    [tab.view addSubview:imageView];
//    
//    [UIView animateWithDuration:2 delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//        imageView.alpha = 0;
//        
//    } completion:^(BOOL finished) {
//        
//        [imageView removeFromSuperview];
//    }];
    
    
    
    
    [tab release];
    [homeVC release];
    [homeNaVC release];
    [findVC release];
    [findNaVC release];
    [userNaVC release];
    [userVC release];
    [searchVC release];
    [searchNaVC release];

    [ZLPSaveTool isHaveFolder];
    
    [[ZLPDataBaseHandle shareDataBaseHandle] openDB];
    [[ZLPDataBaseHandle shareDataBaseHandle] createTable];
    
    [UMSocialData setAppKey:@"563814b567e58efbc7003c2c"];
    
    
    //    plist文件显示出的前缀名是CF
    //    设置一个key是版本号的key
    NSString *key = (NSString *)kCFBundleVersionKey;
    //    NSLog(@"%@",key);
    NSString *homePath = NSHomeDirectory();
    NSLog(@"%@",homePath);
    //    NSLog(@"homePath=%@",homePath);
    //从plist文件汇总取出版本号【沙盒中】
    NSString *version = [NSBundle mainBundle].infoDictionary[key];
    // NSLog(@"%@",version);
    //2、从沙盒中取出上次储存的版本号
    //只是存储一个key【用最简单的存储方式】
    NSString *saveVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    
    //判断是是不是第一次用【是否一样】
    if ([version isEqualToString:saveVersion])
    {
        //不是第一次使用这个版本
        //显示状态栏
        application.statusBarHidden = NO;
    }
    else
    {
        // 版本号不一样：第一次使用新版本
        // 将新版本号写入沙盒
        [[NSUserDefaults standardUserDefaults]setObject:version forKey:key];
        //synchronize同步
        [[NSUserDefaults standardUserDefaults]synchronize];
        //显示启动动画页面
        self.window.rootViewController = [[ZLPGuideViewController alloc]init];
    }
    
//    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina completion:^(UMSocialResponseEntity *response) {
//        
//        if (![response.data[@"platform_error"] isEqualToString:@"(null)"]) {
//            [ZLPIsLoginHandle shareZLPIsLoginHandle].picStr = response.data[@"profile_image_url"];
//            [ZLPIsLoginHandle shareZLPIsLoginHandle].isLogin = YES;
//        }
//        
//    }];
//    
//    
//    
//    if ([[UMSocialData defaultData].socialAccount objectForKey:UMShareToSina] != nil) {
//        NSLog(@"有授权");
//    } else {
//        NSLog(@"没有授权");
//    }
   

//    [self monitorNetwork];
    
    return YES;
}


#pragma mark network

- (void)monitorNetwork
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        NSDictionary *userInfo = @{@"netStatus": @(status)};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationNetStatus" object:self userInfo:userInfo];
    }];
    
    [manager startMonitoring];
}





- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
